package com.webamc.controller;

import com.webamc.common.enums.Fields;
import com.webamc.config.StaticData;
import org.apache.log4j.Logger;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * Created by Prash.
 * User: Choti
 * Date: Mar 14, 2009
 * Time: 2:59:59 AM
 * To change this template use File | Settings | File Templates.
 */
public final class AccessFilter implements Filter {
    private FilterConfig filterConfig = null;
    private Logger log = Logger.getLogger(AccessFilter.class);


    public void init(FilterConfig cfg) throws ServletException {
        this.filterConfig = cfg;
    }

    public void destroy() {
        this.filterConfig = null;
    }

    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
        // if session/or user is not presnt - redirect to LoginAction
        // if url pattern is create/update , then check role of the user shd be defined

        /* HttpServletRequest request = (HttpServletRequest) req;
                HttpSession session = request.getSession(false);
                User u;
                if (session == null || (u = (User) session.getAttribute(Params.SESSIONUSER)) == null) {
        //            request.setAttribute(Params.MSGS, loginMsg);
        //            redirectTo(toLoginPage, request, res);
        //            return;
                } else {
                }
        */
        StaticData.instance().getOneD(Fields.CITY);
        chain.doFilter(req, res);
    }

    private void redirectTo(String target, HttpServletRequest req, ServletResponse res) throws IOException, ServletException {
        log.info("fwd to " + target);
        RequestDispatcher rd = req.getRequestDispatcher(target);
        rd.forward(req, res);
    }
}
